﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Data;

namespace Square.Activity
{
    public class SquareActivityViewer : WebControl
    {
        private SquareActivities sType;
        SquareActivityHelper sHelper;
        public SquareActivities SquareActivityType
        {
            set { sType = value; }
        }

        public SquareActivityViewer()
        {
            SquareActivityHelper squareAct = new SquareActivityHelper();
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            DataSet ds = null;
            sHelper = new SquareActivityHelper();
            ds = sHelper.GetActivity(this.Page, sType);
            int cnt = 0;

            //writer.Write("<script type=\"text/javascript\" src=\"/_layouts/Square/js/jquery-1.4.2.min.js\"></script>");
            writer.Write("<script type=\"text/javascript\" src=\"/_layouts/Square/js/jquery.jcarousel.js\"></script>");
            writer.Write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/_layouts/Square/css/skin.css\" />");
            writer.Write("<script type=\"text/javascript\">");
            writer.Write(@"jQuery(document).ready(function () {
                            jQuery('#mycarousel').jcarousel({
                                vertical: true,
                                wrap: 'last',
                                auto: 7,
                                scroll: 1,
                                buttonNextHTML: null,
                                buttonPrevHTML: null
                            });
                        });");
            writer.Write("</script>");

            writer.Write("<div id='mg-container'>");
            writer.Write("  <ul id='mycarousel' class='jcarousel jcarousel-skin-tango' >");
            if (ds.Tables.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (cnt == 7) break;
                    writer.Write("<li >");
                    writer.Write("    <div class='mg-info-wrap'>");
                    writer.Write("        <div class='mg-info'>");
                    writer.Write("            <table class='mg-info-table'>");
                    writer.Write("                <tr>");
                    writer.Write("                    <td>");
                    writer.Write("                        <span class='mg-duty'>" + MakeDisplayValue(dr["Publisher"] as string, dr["PublisherLink"] as string, dr["Value"] as string) + "</span>");
                    writer.Write("                    </td>");
                    writer.Write("                </tr>");
                    writer.Write("                <tr>");
                    writer.Write("                    <td colspan='2'>");
                    writer.Write("                        <span class='mg-name'>" + dr["ActivityDate"] + "</span>");
                    writer.Write("                        <span class='mg-name'>" + dr["ActivityTime"] + "</span>");
                    writer.Write("                    </td>");
                    writer.Write("                </tr>");
                    writer.Write("            </table>");
                    writer.Write("        </div>");
                    writer.Write("    </div>");
                    writer.Write("</li>");
                    cnt++;
                }
            }
            writer.Write("  </ul>");
            writer.Write("</div>");

            base.RenderControl(writer);
        }

        private string MakeDisplayValue(string strPublisher, string strPublisherLink, string strValue)
        {
            string ret = string.Empty;
            if (strPublisher.Contains('\\'))
            {
                strPublisher = strPublisher.Split('\\')[1] as string;
            }

            ret = "<a href='" + strPublisherLink + "'>" + strPublisher + "</a>" + strValue;

            return ret;
        }
    }
}
